//微信小程序模板js

var util = require("../../utils/util.js");
var dmNetwork = require("../../utils/network.js");
var collector = require("../../utils/collector.js");
var wxVeison = wx.getSystemInfoSync();

var mineJobData = {};
Page({
  data: {
    // 页面配置
    projectData: [],
    isShowNoWork: false,
    isShowAtten: false,
    attendances: [],
    showModalStatus: false,
    projectNameArray: {},
    //当前项目id
    currentProjectId: 0,
    currentTeamId: 0,
    isNewProject: false,
    // 全部展开
    allApplicationPopupAnimation: {},
    allApplicationPopup:false,
    allApplicationList:[
      {
        aid: 1,
        name: '添加日程',
        icon: '../../image/add-one.png',
        url: '/pages/scheduling/scheduling'
      },
      {
        aid: 2,
        name: '考勤统计',
        icon: '../../image/analysis.png',
        url: '/pages/scheduling/scheduling'
      },
      {
        aid: 3,
        name: '请假',
        icon: '../../image/add-one.png',
        url: '/pages/scheduling/scheduling'
      },
      {
        aid: 4,
        name: '加班申请',
        icon: '../../image/add-one.png',
        url: '/pages/scheduling/scheduling'
      },
      {
        aid: 5,
        name: '补卡申请',
        icon: '../../image/add-one.png',
        url: '/pages/scheduling/scheduling'
      },
      {
        aid: 6,
        name: '离职申请',
        icon: '../../image/add-one.png',
        url: '/pages/scheduling/scheduling'
      }
    ],
    // wifi打卡
    macInfo: { // 打卡
      mac: [],
      is_allow_punch: 0,
      punch_type: 0 //0：GPS 打卡 1：WIFI打卡
    },
    recordList:[],
    // 打卡详情
    dataFormDetail:null,
    currentYMD:'',

  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数,
    //获取项目id
    this.getProjectList();
  },
  onShow: function () {
    // 页面显示
  },
  onReady() {
    // this.getProjectList();
  },

  getProjectList: function () {
    var that = this;
    dmNetwork.getInBackground(
      dmNetwork.proJectList,
      { team_id: 0, status: 1 },
      (res) => {
        if (res.data.errno == 0) {

          if (res.data.data.list.length > 0) {
            var projectNameList = {};
            
            for (var i = 0; i < res.data.data.list.length; i++) {
              if(this.data.currentProjectId == 0){
                this.setData({
                  currentProjectId: res.data.data.list[i].project_id,
                  currentTeamId: res.data.data.list[i].team_id,
                });
              }
              projectNameList[res.data.data.list[i].project_id] ={
                logo: res.data.data.list[i].logo,
                name: res.data.data.list[i].name,
                project_id: res.data.data.list[i].project_id,
                team_id: res.data.data.list[i].team_id,
                is_attend: res.data.data.list[i].is_attend,

              };
            }

            that.setData({
              projectData: res.data.data.list,
              projectNameArray: projectNameList,
              isNewProject:projectNameList[this.data.currentProjectId].is_attend
            });

            if (that.data.currentProjectId) {
              that.getMyworkInfo();
              // that.getAttendanceRecordList()
            }
          } else {
          }
        } else if (res.data.errno == 104) {
          wx.reLaunch({
            url: "../uc/login/login",
          });
        }
      },
      (err) => {
        //网络异常处理
        this.setData({
          isShowNetWorkError: true,
        });
      }
    );
  },

  powerDrawer: function (e) {
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu);
  },
  allApplication:function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    this.util(currentStatu, 'allApplicationPopup');
  },

  util: function (currentStatu,popup = 'showModalStatus') {
    /* 动画部分 */
    // 第1步：创建动画实例

    var animation = wx.createAnimation({
      duration: 200, //动画时长
      timingFunction: "linear", //线性
      delay: 0, //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是240px)，停
    animation.translateY(393).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      animationData: animation.export(),
    });

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(
      function () {
        // 执行第二组动画：Y轴不偏移，停
        animation.translateY(0).step();
        // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
        this.setData({
          animationData: animation,
        });

        //关闭抽屉
        if (currentStatu == "close") {
          this.setData({
            [popup]: false,
          });
        }
      }.bind(this),
      200
    );

    // 显示抽屉
    if (currentStatu == "open") {
      this.setData({
        [popup]: true,
      });
    }
  },
   // 打开弹框
   openPopup(e) {
    const text = e.currentTarget.dataset.text
    const animationText = e.currentTarget.dataset.animation
    // 第1步：创建动画实例
    const animation = wx.createAnimation({
      timingFunction: 'ease-in-out',
      delay: 0, //0则不延迟
    });
    animation.translateY('100%').step();
    const animationData = animation.export();
    this.setData({
      [animationText]:animationData
    })
    // 显示弹框容器
    this.setData({ [text]: true }, () => {
      // 创建动画实例
      const animation = wx.createAnimation({
        duration: 300, // 动画时长
        timingFunction: 'ease-out', // 缓动效果
      });
      // 弹框从底部滑出
      animation.translateY(0).step();
      this.setData({
        [animationText]: animation.export(),
      });
    });
  },
  // 关闭弹框
  closePopup(e) {
    const text = e.currentTarget.dataset.text
    const animationText = e.currentTarget.dataset.animation
    // 创建动画实例
    const animation = wx.createAnimation({
      duration: 300, // 动画时长
      timingFunction: 'ease-in', // 缓动效果
    });
    // 弹框滑回到底部
    animation.translateY('100%').step();
    this.setData({
      [animationText]: animation.export(),
    });
    setTimeout(() => {
      // 动画结束后隐藏弹框
      this.setData({ [text]: false });
    }, 500);
  },
  goOperate: function () {
    wx.showToast({
      title: "敬请期待",
      icon: "none",
    })
    return;
    wx.navigateTo({
      url: "/pages/schedulingClockIn/operate/operate",
    });
  },

  //切换项目
  changeProject: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    this.setData({
      currentProjectId: that.data.projectData[index].project_id,
      currentTeamId: that.data.projectData[index].team_id,
      isNewProject:that.data.projectData[index].is_attend
    });
    that.setData({
      currentYMD:''
    })
    this.getMyworkInfo();
    // that.getAttendanceRecordList()
    this.util("close");
    if (util.compareVersion("1.9.0", wxVeison.SDKVersion) != 1) {
      setTimeout(function () {
        wx.showTabBar({});
      }, 300);
    }
  },

  //获取今日工作数据
  getMyworkInfo: function () {
    wx.showLoading({
      title: "加载中",
    });
    var that = this;
    var project_id = this.data.currentProjectId;
    var team_id = this.data.currentTeamId;
    dmNetwork.getInBackground(
      dmNetwork.mywork,
      {
        team_id,
        project_id,
      },
      (res) => {
        var isShowEarlyClock = false;
        if (res.data.errno == 0) {
          mineJobData = res.data.data;
          const {
            attendance: {
              extra_info: { task_id, work_type },
              attendance_list,
            } = {},
            is_attend_upload,
            form: { show } = {},
            sign: { show: signShow, list: signList } = {},
          } = mineJobData;
          const hasWork = task_id != -1 || show == 1 || signShow == 1;
          const alist = attendance_list;
          var aresult = [];
          for (var i = 0; i < alist.length; i += 2) {
            var attendance = {};
            attendance.sdata = alist[i];
            attendance.edata = alist[i + 1];
            attendance.startTime = alist[i].require_time;
            attendance.endTime = alist[i + 1].require_time;
            attendance.sAttendTime = alist[i].attend_time;
            attendance.eAttendTime = alist[i + 1].attend_time;
            attendance.sAttendanceBtn = alist[i].attendance_btn;
            attendance.eAttendanceBtn = alist[i + 1].attendance_btn;
            attendance.isShowSAttendTime = true;
            attendance.isShowEAttendTime = true;
            attendance.attendance_btn = alist[i].attendance_btn;
            attendance.rest_btn = alist[i].rest_btn;
            attendance.schedule_type = alist[i].schedule_type;
            if (
              alist[i].attend_time == "" &&
              alist[i].attendance_btn != 1 &&
              (work_type == 0 || work_type == 2)
            ) {
              isShowEarlyClock = true;
            }
            attendance.isShowEarlyClock = isShowEarlyClock;
            if ("" == alist[i].attend_time) {
              // debugger
              attendance.isShowSAttendTime = false;
              attendance.sItemWidth = "0rpx";
            } else {
              attendance.sItemWidth = "100%";
            }
            if (0 == alist[i].attendance_btn && attendance.sAttendTime == "") {
              attendance.isShowSAttendTime = false;
            }

            if ("" == alist[i + 1].attend_time) {
              attendance.isShowEAttendTime = false;
              attendance.eItemWidth = "0rpx";
            } else {
              attendance.eItemWidth = "100%";
            }

            if (1 == alist[i].attendance_btn) {
              attendance.eItemWidth = "0rpx";
            } else {
              attendance.eItemWidth = "100%";
            }
            if (
              0 == alist[i + 1].attendance_btn &&
              attendance.eAttendTime == ""
            ) {
              attendance.isShowEAttendTime = false;
            }
            attendance.sStatus = alist[i].status;
            attendance.eStatus = alist[i + 1].status;
            attendance.scross = alist[i].cross;
            attendance.scross_attend = alist[i].cross_attend;
            attendance.ecross = alist[i + 1].cross;
            attendance.ecross_attend = alist[i + 1].cross_attend;

            if (
              attendance.sStatus.length == 1 &&
              attendance.sStatus[0].value == 1
            ) {
              attendance.sStatusImg = "../../image/ic_attendance_success.png";
              attendance.sStatus[0].class =
                "item-attendance-text-state-success";
            } else {
              attendance.sStatusImg = "../../image/ic_attendance_error.png";
              for (var j = 0; j < attendance.sStatus.length; j++) {
                attendance.sStatus[j].class =
                  "item-attendance-text-state-error";
              }
            }
            if (
              attendance.eStatus.length == 1 &&
              attendance.eStatus[0].value == 1
            ) {
              attendance.eStatusImg = "../../image/ic_attendance_success.png";
              attendance.eStatus[0].class =
                "item-attendance-text-state-success";
            } else {
              attendance.eStatusImg = "../../image/ic_attendance_error.png";
              for (var j = 0; j < attendance.eStatus.length; j++) {
                attendance.eStatus[j].class =
                  "item-attendance-text-state-error";
              }
            }

            aresult.push(attendance);
          }
          const cLocationTime = signList.length
            ? util.formatTimeHM(new Date(1000 * parseInt(signList[0].time)))
            : "";
          // that.setData({
          //   minejob: res.data.data,
          //   isShowNoWork: !hasWork,
          //   isShowAtten: -1 != mineJobData.attendance.extra_info.task_id,
          //   attendances: aresult,
          //   project_id: project_id,
          //   currentProjectId: project_id,
          //   team_id: team_id,
          //   group_id: res.data.data.group_id,
          //   formData: res.data.data.form,
          //   cLocationTime,
          //   is_attend_upload,
          //   isShowNote: 0 < mineJobData.notice,
          //   ["macInfo.mac"]: mineJobData.attendance.extra_info.mac || [],
          //   ["macInfo.is_allow_punch"]:
          //     mineJobData.attendance.extra_info.is_allow_punch || 0,
          //   ["macInfo.punch_type"]:
          //     mineJobData.attendance.extra_info.punch_type || 0,
          //     has_change_store:res.data.data.has_change_store
          // });
          console.log('aavasdvvvvvvvvvvvvvv', aresult)

          // 打卡记录
          that.getAttendanceRecordList().then(data=>{
            data.forEach((v,i) => {
              if(v.date == util.formatTimeDate2(new Date())){
                v.attendances = aresult
              }
            });
            console.log('dataaaaaaaaaaaaaaaaaaaaa', data)
            that.setData({
              currentYMD:util.formatTimeDate2(new Date()),
              recordList:data,
              minejob: res.data.data,
              isShowNoWork: !hasWork,
              isShowAtten: -1 != mineJobData.attendance.extra_info.task_id,
              attendances: aresult,
              project_id: project_id,
              currentProjectId: project_id,
              team_id: team_id,
              group_id: res.data.data.group_id,
              formData: res.data.data.form,
              cLocationTime,
              is_attend_upload,
              isShowNote: 0 < mineJobData.notice,
              ["macInfo.mac"]: mineJobData.attendance.extra_info.mac || [],
              ["macInfo.is_allow_punch"]:
                mineJobData.attendance.extra_info.is_allow_punch || 0,
              ["macInfo.punch_type"]:
                mineJobData.attendance.extra_info.punch_type || 0,
                has_change_store:res.data.data.has_change_store,
            },()=>{
              // const id = `item_${util.formatTimeDate2(new Date())}`
              // const query = wx.createSelectorQuery()
              // query.select('#' + id).boundingClientRect()
              // query.selectViewport().scrollOffset()
              // query.exec(function(res){
              //   console.log('queryqueryqueryquery', res)
              //   res[0].top = 0
              // })
            })
            
          })

          // 如果 从用户工作列表页面接口(/sea/api/1.0/client/v1/project/get/mywork)获取的ranking.show==‘1’才去请求获取考勤排名接口(/sea/api/1.0/client/v1/attendance/ranking) modified by ltl2018.07.23
          if (res.data.data.ranking.show == "1") {
            dmNetwork.getMyworkRanking(
              dmNetwork.myworkRanking,
              {
                team_id: team_id,
                project_id: project_id,
              },
              (res) => {
                wx.hideLoading();
                if (res.data.errno == 0) {
                  // 表示成功返回
                  that.setData({
                    rankingNum: res.data.data.ranking,
                  });
                } else {
                  wx.showToast({
                    title: res.data.errmsg,
                    mask: true,
                    duration: 1500,
                    icon: "none",
                  });
                }
              },
              (err) => {
                wx.hideLoading();
                console.log("获取考勤排名接口err:", err);
              }
            );
          }else{
            wx.hideLoading();
          }
        } else {
          wx.hideLoading();
          wx.showToast({
            title: res.data.errmsg,
            mask: true,
            duration: 1500,
            icon: "none",
          });
        }
      },
      (err) => {
        //网络异常处理
      }
    );
  },
  //获取打卡记录
  getAttendanceRecordList: function () {
    var that = this;
    var project_id = this.data.currentProjectId;
    var team_id = this.data.currentTeamId;
    return new Promise((resolve, reject) => {
      dmNetwork.getInBackground(dmNetwork.attendanceRecordList,{
        team_id,
        project_id,
        pageNum: 1,
        pageSize: 99999
      },(res) => {
        console.log('kalskjkdfs', res)
        if(res.data.errno == 0){
          resolve(res.data.data.results)
          // that.setData({
          //   recordList:res.data.data.results
          // })
  
        }else {
          wx.showToast({
            title: res.data.errmsg,
            mask: true,
            duration: 1500,
            icon: "none",
          });
        }
      },(err) => {
          //网络异常处理
        }
      );
    });
    
  },

  onClickSAttendanceItem: async function (e) {
    var currentStatu = e.currentTarget.dataset.statu;

    if (currentStatu.attend_time == "") {
      return;
    }
    const item = e.currentTarget.dataset.item;
    if (
      currentStatu.abnormal_reason != undefined &&
      currentStatu.abnormal_reason != ""
    ) {
      item.push({
        title: "异常原因",
        value: currentStatu.abnormal_reason,
        type: "Textarea",
      });
    }

    console.log('daasdfdf',currentStatu);
    var that = this;
    wx.setStorage({
      key: "a_extra_info",
      data: mineJobData.attendance.extra_info,
    });
    wx.setStorage({
      key: "attendance_detail",
      data: item,
    });

    let mac_address = "";
    if (that.data.macInfo.punch_type == 1) {
      const res = await that.getWifiInfo();
      mac_address = res.mac_address;
    }
    console.log("macccc", mac_address);
    const dataFormDetail = {
      title:'打卡详情',
      team_id:this.data.team_id,
      project_id:this.data.project_id,
      mac_address,
      type:1,
      attendance_id:currentStatu.attendance_id,
      form_data_id:currentStatu.form_data_id,
      time_id:currentStatu.time_id,
      task_id:mineJobData.attendance.extra_info.task_id,
      schedule_id:currentStatu.schedule_id,
      is_attend_upload:that.data.is_attend_upload,
      cross:currentStatu.cross,
      cross_attend:currentStatu.cross_attend,
      task_id_yesterday:mineJobData.attendance.extra_info.task_id_yesterday,
      photo:currentStatu.photo,
    }
    this.setData({
      dataFormDetail
    },()=>{
      wx.navigateTo({
        url:
          "../dataform/detail/detail?title=打卡详情&team_id=" +
          this.data.team_id +
          "&mac_address=" +
          mac_address +
          "&project_id=" +
          this.data.project_id +
          "&type=1" +
          "&team_id=" +
          this.data.team_id +
          "&project_id=" +
          this.data.project_id +
          "&attendance_id=" +
          currentStatu.attendance_id +
          "&form_data_id=" +
          currentStatu.form_data_id +
          "&time_id=" +
          currentStatu.time_id +
          "&task_id=" +
          mineJobData.attendance.extra_info.task_id +
          "&schedule_id=" +
          currentStatu.schedule_id +
          "&is_attend_upload=" +
          that.data.is_attend_upload +
          "&cross=" +
          currentStatu.cross +
          "&cross_attend=" +
          currentStatu.cross_attend +
          "&task_id_yesterday=" +
          mineJobData.attendance.extra_info.task_id_yesterday,
      });
    })
    // return
    
  },

  //休息打卡//"attendance_btn": 0,  上班卡已打  1未打  time_id传3和4 开始休息和结束休息 schedule_type=1就是固定休息时间 2是弹性休息时间 
  // rest_btn 字段 0 未打 1代表已打休息开始的卡
  handleClickBeginRestBtn:function(e) {
    var that = this;
    collector.saveFormid(e.detail.formId);
    collector.uploadFormid();
    var currentStatu = e.currentTarget.dataset.statu;

    let time_id = e.currentTarget.dataset.timeid;
    let mac_address = "";
    wx.setStorage({
      key: "a_extra_info",
      data: mineJobData.attendance.extra_info,
      success: async (stora) => {
        
        if (that.data.macInfo.punch_type == 1) {
          console.log("asdfffffffffffffffffffff");
          const res = await that.getWifiInfo();
          console.log("maccccccccccccccccccccc", res);
          mac_address = res.mac_address;
        }
        //处理跳转逻辑

        wx.navigateTo({
          url:
            "../dataform/dataform?type=1&title=考勤信息填写&team_id=" +
            this.data.team_id +
            "&project_id=" +
            this.data.project_id +
            "&mac_address=" +
            mac_address +
            "&attendance_id=" +
            currentStatu.attendance_id +
            "&time_id=" +
            time_id +
            "&task_id=" +
            mineJobData.attendance.extra_info.task_id +
            "&schedule_id=" +
            currentStatu.schedule_id +
            "&is_attend_upload=" +
            that.data.is_attend_upload +
            "&cross=" +
            currentStatu.cross +
            "&form_data_id=" +
            currentStatu.form_data_id +
            "&cross_attend=" +
            currentStatu.cross_attend +
            "&task_id_yesterday=" +
            mineJobData.attendance.extra_info.task_id_yesterday +
            "&count=" +
            1 + 
            "&clockInType=" + 'rest',
        });
      },
      fail: function () {
        console.error("存储token时失败");
      },
    });

  },

   //上下班打卡
  onClickSAttendanceBtn: function (e) {
    console.log("onClickSAttendanceBtn", e);
    var that = this;
    collector.saveFormid(e.detail.formId);
    collector.uploadFormid();
    var currentStatu = e.currentTarget.dataset.statu;
    let mac_address = "";
    wx.setStorage({
      key: "a_extra_info",
      data: mineJobData.attendance.extra_info,
      success: async (stora) => {
        if (that.data.macInfo.punch_type == 1) {
          console.log("asdfffffffffffffffffffff");
          const res = await that.getWifiInfo();
          console.log("maccccccccccccccccccccc", res);
          mac_address = res.mac_address;
        }
        //处理跳转逻辑
        const dataFormDetail = {
          title:'打卡详情',
          team_id:this.data.team_id,
          project_id:this.data.project_id,
          mac_address,
          type:1,
          attendance_id:currentStatu.attendance_id,
          form_data_id:currentStatu.form_data_id,
          time_id:currentStatu.time_id,
          task_id:mineJobData.attendance.extra_info.task_id,
          schedule_id:currentStatu.schedule_id,
          is_attend_upload:that.data.is_attend_upload,
          cross:currentStatu.cross,
          cross_attend:currentStatu.cross_attend,
          task_id_yesterday:mineJobData.attendance.extra_info.task_id_yesterday,
          photo:currentStatu.photo,
        }
        this.setData({
          dataFormDetail
        },()=>{
          wx.navigateTo({
            url:
              "../dataform/dataform?type=1&title=考勤信息填写&team_id=" +
              this.data.team_id +
              "&project_id=" +
              this.data.project_id +
              "&mac_address=" +
              mac_address +
              "&attendance_id=" +
              currentStatu.attendance_id +
              "&time_id=" +
              currentStatu.time_id +
              "&task_id=" +
              mineJobData.attendance.extra_info.task_id +
              "&schedule_id=" +
              currentStatu.schedule_id +
              "&is_attend_upload=" +
              that.data.is_attend_upload +
              "&cross=" +
              currentStatu.cross +
              "&form_data_id=" +
              currentStatu.form_data_id +
              "&cross_attend=" +
              currentStatu.cross_attend +
              "&task_id_yesterday=" +
              mineJobData.attendance.extra_info.task_id_yesterday +
              "&count=" +
              1 +
              "&sourcePage=1"
          });
        })

        
      },
      fail: function () {
        console.error("存储token时失败");
      },
    });
  },

  onClickAttendanceList: function(e){
    const url = this.data.isNewProject ? "../attdance/record_new/record_new" : "../attdance/record/record";
    wx.navigateTo({
      url:`${url}?team_id=${this.data.currentTeamId}&project_id=${this.data.currentProjectId}`
    });
  },
  // wifi 打卡
  getWifiInfo() {
    const that = this;
    return new Promise((resolve, reject) => {
      wx.startWifi({
        success(res) {
          console.log("startWifi....", res);
          // wx.getWifiList({
          //   success (res) {
          //     wx.onGetWifiList(function (wifiList) {
          //       console.log('fffffff', wifiList.wifiList)
          //       console.log('dddddd', wifiList.wifiList.filter(res => {
          //         return res.SSID === 'doumi-office'
          //       }))
          //     })
          //   }
          // })
          wx.getConnectedWifi({
            success(res) {
              console.log("vvvvvvvvvvvvvv", res);
              if (res.errMsg == "getConnectedWifi:ok") {
                const { BSSID, SSID } = res.wifi || {};
                const findIndex = that.data.macInfo.mac.findIndex((v) => {
                  return v.mac_address === BSSID;
                });
                if (findIndex > -1) {
                  resolve({ mac_address: BSSID });
                } else {
                  wx.showToast({
                    title: "请连接正确的WIFI",
                    mask: true,
                    duration: 1500,
                    icon: "none",
                  });
                  reject();
                }
                // this.pushInWifi(res.wifi)
              } else {
                wx.showToast({
                  title: "请检查WIFI网络",
                  mask: true,
                  duration: 1500,
                  icon: "none",
                });
                reject();
              }
            },
            fail(err) {
              wx.showToast({
                title: "请检查WIFI网络",
                mask: true,
                duration: 1500,
                icon: "none",
              });
              reject();
            },
          });
        },
      });
    });
  },



});